//
//  MyShopVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/12/12.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "MyShopVC.h"
#import "MyShopHeadView.h"
#import "UIView+TYAlertView.h"
#import "MyShopSearchView.h"
#import "MyShopDemandTabVC.h"
#import "MyShopServiceTabVC.h"
#import "ShopModel.h"
#import "CustomeEvaluate.h"
#import "EditingShopTabVC.h"
#import "ShopSubModel.h"
#import "DemandTheHallContentVC.h"
#import "ServiceContentTabVC.h"
#import "HomeModel.h"
#import "ServiceModel.h"
#import "MyShopSearchTabVC.h"
#import "ServiceContentTabVC.h"
#import "DemandTheHallContentVC.h"
@interface MyShopVC ()<MyShopSearchViewDelegate,UIScrollViewDelegate,MyShopHeadViewDelegate,MyShopSearchTabVCDelegate>
@property(nonatomic,strong) UIScrollView *scrollView;
@property(nonatomic,strong) MyShopDemandTabVC *demandVC;
@property(nonatomic,strong) MyShopServiceTabVC *serviceVC;
@property(nonatomic,strong) MyShopSearchView *searchView;
@property(nonatomic,strong) MyShopHeadView *headView;
@property(nonatomic,strong) NSMutableArray *shopArray;
@property(nonatomic,strong) NSMutableArray *demandArray;
@property(nonatomic,strong) NSMutableArray *serviceArray;
@property(nonatomic,strong) NSDictionary *dict;

@property(nonatomic,strong) MyShopSearchTabVC *myShopSearchVC;
@end

@implementation MyShopVC
-(NSMutableArray *)shopArray
{
    if (!_shopArray) {
        _shopArray = [NSMutableArray array];
    }
    return _shopArray;
}
-(NSMutableArray *)demandArray
{
    if (!_demandArray) {
        _demandArray = [NSMutableArray array];
    }
    return _demandArray;
}
-(NSMutableArray *)serviceArray
{
    if (!_serviceArray) {
        _serviceArray = [NSMutableArray array];
    }
    return _serviceArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的店铺";
    self.view.backgroundColor = kWhite;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.headView = [MyShopHeadView createViewFromNib];
    self.headView.frame = CGRectMake(0, 0, kVWidth,  140+kNavAndStatusHight+kStatusBarHeight*2);
    self.headView.delegate = self;
    [self.view addSubview:self.headView];
    self.searchView = [[MyShopSearchView alloc]initWithFrame:CGRectMake(0, 140, kVWidth, 50)];
    __weak typeof (self)weakSelf = self;
    [self readShopInfo];
    self.searchView.delegate = self;
    [self.view addSubview:self.searchView];
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.searchView.frame), kVWidth, kVHeight-CGRectGetMaxY(self.searchView.frame)-kNavAndStatusHight)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    self.myShopSearchVC = [[MyShopSearchTabVC alloc]init];
    self.myShopSearchVC.delegate = self;
    self.searchView.index = 3;
    self.myShopSearchVC.view.frame = CGRectMake(0, CGRectGetMaxY(self.searchView.frame), kVWidth, kVHeight-CGRectGetMaxY(self.searchView.frame));
    self.myShopSearchVC.view.hidden = YES;
    [self.view addSubview:self.myShopSearchVC.view];
    self.scrollView.contentSize = CGSizeMake(kVWidth*2, 0);
    self.serviceVC = [[MyShopServiceTabVC alloc]init];
    self.serviceVC.returnValueBlock = ^(ServiceModel * _Nonnull serviceModel) {
        ServiceContentTabVC *vc = [[ServiceContentTabVC alloc]init];
        vc.model = serviceModel;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    self.serviceVC.view.frame = CGRectMake(0, 0, kVWidth, self.scrollView.frame.size.height);
    [self.scrollView addSubview:self.serviceVC.view];
    self.demandVC = [[MyShopDemandTabVC alloc]init];
    self.demandVC.returnValueBlock = ^(HomeModel * _Nonnull homeModel) {
      DemandTheHallContentVC *vc = [[DemandTheHallContentVC alloc]init];
        vc.homeModel = homeModel;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    self.demandVC.view.frame = CGRectMake(kVWidth, 0, kVWidth, self.scrollView.frame.size.height);
    [self.scrollView addSubview:self.demandVC.view];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectMyShopByUid:) name:@"selectMyShopByUid" object:nil];
    [self reloadInternetData];
    
}
-(void)readShopInfo
{
    self.dict = @{
                  @"uid":[UserDefaultsHelper readUid],
                  @"token":[UserDefaultsHelper readToken]
                  };
    XLog(@"店铺self.dict:%@",self.dict);
    [CustomeManager POSTWithURLIndex:50 parameters:self.dict success:^(NSDictionary *dict) {
        
    } failure:^(NSError *error) {
        
    }];
}
-(void)selectMyShopByUid:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"店铺dict:%@",dict);
    NSDictionary *date = [dict objectForKey:@"date"];
    if(date != nil){
        ShopSubModel *model = [ShopSubModel mj_objectWithKeyValues:date];
        [self.shopArray addObject:model];
        
    }
    self.headView.shopArray = self.shopArray;
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(void)reloadInternetData
{
    self.dict = @{
                  @"uid":[UserDefaultsHelper readUid],
                  @"token":[UserDefaultsHelper readToken]
                  };
    [CustomeManager POSTWithURLIndex:50 parameters:self.dict success:^(NSDictionary *dict) {
        
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark-代理:店铺内搜索
- (void)myShopSearchView:(MyShopSearchView *)myShopSearchView withClickBtn:(nonnull UIButton *)clickBtn withSearchText:(nonnull NSString *)searchText
{
    
    XLog(@"clickBtn.tag:%ld",clickBtn.tag);
    if (clickBtn.tag != 5) {
        self.myShopSearchVC.view.hidden = NO;
        self.myShopSearchVC.searchText = searchText;
    }else {
        self.myShopSearchVC.view.hidden = YES;
    }
    
}
#pragma mark-代理:点击 服务列表,需求列表
- (void)myShopSearchView:(MyShopSearchView *)myShopSearchView withClickBtn:(UIButton *)clickBtn
{
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    clickBtn.tag?[self.scrollView setContentOffset:CGPointMake((clickBtn.tag-1)*kVWidth, 0) animated:NO]:0;
    switch (clickBtn.tag) {
        case 0:
        {
            [WSProgressHUD dismiss];
        }
            break;
        case 1:
        {
            [self.serviceVC reloadInternetData];
        }
            break;
        case 2:
        {
            [self.demandVC reloadInternetData];
        }
            break;
        default:
            break;
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x/kVWidth;
    MCButton *btn = (MCButton *)[self.searchView viewWithTag:index+1];
    [self.searchView clickBtn:btn];
}
#pragma mark-代理:点击客户评价,编辑介绍
- (void)myShopHeadView:(MyShopHeadView *)myShopHeadView withBtn:(UIButton *)btn
{
//    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
//    [WSProgressHUD show];
    if (btn.tag == 0) {
        CustomeEvaluate *vc = [[CustomeEvaluate alloc]init];
        vc.title = @"店铺评价";
        [self.navigationController pushViewController:vc animated:YES];
    
    }else {
        EditingShopTabVC *vc = [[EditingShopTabVC alloc]init];
        vc.returnValueBlock = ^(NSInteger selected) {
            self.shopArray = nil;
            [self reloadInternetData];
        };
        vc.model = self.shopArray[0];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark-代理:跳转
-(void)myShopSearchTabVC:(MyShopSearchTabVC *)myShopSearchTabVC withServiceModel:(ServiceModel *)serviceModel withHomeModel:(HomeModel *)homeModel
{
    ServiceContentTabVC *serviceVC = [[ServiceContentTabVC alloc]init];
    DemandTheHallContentVC *demandVC = [[DemandTheHallContentVC alloc]init];
    NSInteger emptyService = [EmptyModel stringIsNullOrEmpty:serviceModel];
    NSInteger emptyDemand= [EmptyModel stringIsNullOrEmpty:serviceModel];
    if (emptyService !=1) {
        serviceVC.model = serviceModel;
        [self.navigationController pushViewController:serviceVC animated:YES];
    }else {
        demandVC.homeModel = homeModel;
        [self.navigationController pushViewController:demandVC animated:YES];
    }
}
@end
